package com.ljy.my_study.java.juc.CyclicBarrier;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/** 
* @author James
* @date 2018年9月10日 
*/
public class TestMain {
	public static void main(String[] args) {
		int count=10;
		ExecutorService executor=Executors.newFixedThreadPool(count);
		final CyclicBarrier barrier=new CyclicBarrier(count+1);
		for(int i=0;i<count;i++) {
			executor.execute(new Runnable() {
				@Override
				public void run() {
					try {
						System.out.println("barrier await start");
						System.out.println("barrier await end");
						barrier.await();
					} catch (InterruptedException | BrokenBarrierException e) {
						e.printStackTrace();
					}
				}
			});
		}
		try {
			barrier.await();
			System.out.println("main barrier await start");
		} catch (InterruptedException | BrokenBarrierException e) {
			e.printStackTrace();
		}
		System.out.println("main barrier await end");
		executor.shutdown();
	}
}
